首先我們先介紹 map 語法,由 key 和 value 組成,
val map : Map<String, String> = new mapOf("key" to "value","key" to "value")
array 語法跟其他程式語言蠻不一樣的,看起來比較複雜
val array: Array<String?> = arrayOfNulls<String>(5)// 因為有 NULL,所以型態是 String?
val array: Array<Int> = IntArray(5)
val array: Array<Int> = arrayOf(1,2,3,4,5)
首先我們寫一個 class 用來處理測驗內容,這邊我們用 map 來存題庫,這邊要注意,因為題庫之後會再擴充,所以我有用 getQuestionLastNumber 這個 methon 來取得 map 的 size,以下是大概的架構:
class Test(){
// map 用來存題目資料
private val _questionHiragana: Map<String, String> = mapOf("a" to "あ","i" to "い","u" to "う","e" to "え","o" to "お",
"ka" to "か","ki" to "き","ku" to "く","ke" to "け","ko" to "こ",
"sa" to "さ","shi" to "し","su" to "す","se" to "せ","so" to "そ",
"ta" to "た","chi" to "ち","tsu" to "つ","te" to "て","to" to "と")
private var _nowQuestion: String = ""
private val _nowChoice: Array<String?> = arrayOfNulls<String>(5)
private var _ansNumber: Int = 0
// 回傳問題
public fun getQusetion(): String{
return _nowQuestion
}
// 回傳答案
public fun getChoice():Array<String?> {
return _nowChoice
}
// 回傳 Map 的最後一格 index
public fun getQuestionLastNumber(): Int{
return _questionHiragana.size - 1
}
// 透過 Index 尋找對應的 key
public fun findMapByIndex(index: Int): String{
var count = 0
for ((key,value) in _questionHiragana){
if(count==index)
return key
count++
}
return ""
}
// 出題,設置 _nowQuestion 和 _nowChoice 的值
public fun setPaper(){
}
// 判斷回答是否正確
public fun isCorrect(number: Int): Boolean{
}
}
這邊說說出題的想法,下面是變數的介紹,
public fun setPaper(){
val mode = (1..2).random()
val index = (0..getQuestionLastNumber()).random()
_ansNumber = (1 .. 4).random()
if(mode == 1){ // 題目是 key,選項是 value
_nowQuestion = findMapByIndex(index)
_nowChoice[_ansNumber] = _questionHiragana[_nowQuestion]
}
else if(mode == 2){ // 題目是 value,選項是 key
_nowChoice[_ansNumber] = findMapByIndex(index)
_nowQuestion = _questionHiragana[_nowChoice[_ansNumber]].toString()
}
// 以下是處理對其他選項加入錯誤的答案
val otherChoice:Array<String?> = getOtherChoice(mode,index)
var count = 0
for(number in _nowChoice.indices){
if(number==_ansNumber)
continue
_nowChoice[number] = otherChoice[count]
count++
}
}
為了方便製作選擇題,我又加了一個新的 methon 叫做 getOtherChoice,我想要用它直接給我三個跟答案不重複的選項,最後再用迴圈填入選項
public fun getOtherChoice(mode: Int, index: Int): Array<String?>{
val otherChoice: Array<String?> = arrayOfNulls<String>(3)
var count = 0
while(count <= 2){
var otherIndex = (0..getQuestionLastNumber()).random()
if(otherIndex==index)
continue
if(mode==1){ // 題目是 key,選項是 value
otherChoice[count] = _questionHiragana[findMapByIndex(otherIndex)]
}
else if(mode==2){ // 題目是 value,選項是 key
otherChoice[count] = findMapByIndex(otherIndex)
}
count++
}
return otherChoice
}
Class 除了 isCorrect 之外都寫完了,尚未 debug,目前只有 Model 的部分,還沒有寫 View。
今天先寫到這邊,那因為上面的程式碼都沒有經過測試,所以我沒辦法保證 100% 正確,如果自己有跟著一起做,可以嘗試直接跟 View 做連接,看看有沒有問題。
跟大家分享一件事,鐵人賽我都是用 Notion 來寫的,結果今天早上我不小心手殘,把 workspace 整個刪掉了,我的所有筆記、文章全都不見,真的很可怕,好在 Notion 客服非常的給力,早上發求助訊息出去,下午就幫我把整個 workspace 回復了,真的很感謝偉大的客服、感謝偉大的 Notion 都會幫用戶備份資料!
如果大家有遇到類似的問題,可以直接寄信或是點擊 Notion 右下角的問號,Message support > Access & recovery > ...之類的,接下來客服會寄信給你,稍待片刻後就會得到幫助,超棒的 ~
下一篇會先測試 Class 有沒有問題。
Kotlin - Map
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/
【Kotlin】第5課-集合(List、Set、Map)
https://chikuwa-tech-study.blogspot.com/2021/05/kotlin-list-set-map.html